<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="题库名称">
        <template slot-scope="{ row }">
          <span class="cup" @click="handleEdit(row)">{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="题库状态">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status == 1" size="small">正常</el-tag>
          <el-tag v-if="row.status == 2" type="danger" size="small"
            >禁用</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="permission" label="访问权限">
        <template slot-scope="{ row }">
          <el-tag v-if="row.permission == 1" size="small">完全私有</el-tag>
          <el-tag v-if="row.permission == 2" type="success" size="small"
            >只读共享</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="subjectsCount" label="试题数量"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button type="primary" size="mini" @click="handleEdit(row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="handleDelete(row)"
              >删除</el-button
            >
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
export default {
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
      },
      tableData: [],
      pageObj: {},
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      let res = await this.$store.dispatch(
        'testQuestionBankList',
        this.listQuery,
      )
      this.tableData = res.data
      this.pageObj = res
    },
    handleEdit(row) {
      this.$store.commit('SET_ACTIVEROW', row)
      this.$store.commit('SET_ACTIVENAME', 'create')
    },
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    async delete(row) {
      await this.$store.dispatch('testQuestionBankDel', row)
      this.getList()
    },
  },
}
</script>